import { Effect } from 'dva';
import { Reducer } from 'redux';
import { produce, Draft } from 'immer';

import { deviceWarnedTimes, deviceWarnedList } from '@/services/callPolice';

// 设备报警数据信息
export interface DeviceWarnItem {
  dayCount: string; // 今日设备报警量
  monthCount: string; // 近一月设备报警量
  weekCount: string; // 近一周设备报警量
}
// 设备报警信息列表
export interface DeviceWarnListItem {
  deviceName: string;
  typeName: string;
  deviceTypeNo: number;
  deviceId: number;
  treeName: string;
  happenTime: string;
  alamContent: string;
  createTime: string;
  deviceAlamId: number;
}

export interface StateType {
  deviceWarnedTimes: DeviceWarnItem; // 设备报警信息
  deviceWarnedList: DeviceWarnListItem[]; // 设备报警信息列表
}

export interface DeviceWarnItemModelType {
  namespace: string;
  state: StateType;
  effects: {
    deviceWarnedTimes: Effect;
    deviceWarnedList: Effect;
  };
  reducers: {
    change_deviceWarnedTimes: Reducer<StateType>;
    change_deviceWarnedList: Reducer<StateType>;
  };
}
const Model: DeviceWarnItemModelType = {
  namespace: 'deviceWarn',

  state: {
    deviceWarnedTimes: {},
    deviceWarnedList: [],
  },

  effects: {
    *deviceWarnedTimes({ payload }, { call, put }) {
      const [err, data] = yield call(deviceWarnedTimes, payload);
      if(!err) yield put({ type: 'change_deviceWarnedTimes', payload: data });
    },
    *deviceWarnedList({ payload }, { call, put }) {
      const [err, data] = yield call(deviceWarnedList, payload);
      if(!err) yield put({ type: 'change_deviceWarnedList', payload: data });
    },
  },

  reducers: {
    change_deviceWarnedTimes: produce((draft: Draft<StateType>, { payload }): void => {
      draft.deviceWarnedTimes = payload;
    }),
    change_deviceWarnedList: produce((draft: Draft<StateType>, { payload }): void => {
      draft.deviceWarnedList = payload;
    }),
  },
};
export default Model;
